clear all
version 16.1
graph drop _all
set more off
set type double
cap mkdir ../fishertest


/* biddata+firmIDdata読み込み*/
use "../data/biddata020119.dta", clear
merge 1:1 rid using "../data/rid_address_license_firmID_020119.dta"
drop _merge
/* data period 2015-2018*/
	keep if fy_con > 2014 & fy_con < 2018
/* fill brank firm address and license no.*/ 
	replace ad_firm = ad_firm_new if ad_firm=="" & ad_firm_new ~= ""
	replace no_license = no_license_new if no_license == . & no_license_new < .
	drop *_new
	drop tend_date cont_date
	drop second_round rank1_q rank1
/* recounstruct regionfirmTG based on bigger sample*/
	egen regionfirmTG = tag(region firmID)

/* partic,mintsをfirmtestsから取得*/
	merge 1:1 rid region using "../firmleveltest/test_sample_with_results.dta"
	*merge 1:1 rid region using "../firmleveltest24Apr24/test_sample_with_results.dta"
	drop regionfirmID - ftt_hr2p rank1_q - neg_ftt_ts1s tsrank
	*partic,mints1のどっとを埋める
	sort region firmID partic
	by region firmID: replace partic = partic[_n-1] if partic[_n-1] < . & _n > 1
	by region firmID: replace mints1 = mints1[_n-1] if mints1[_n-1] < . & _n > 1
	*format %40s ad* nm*
	*br tsrank fy_con nm_firm ad_firm* no_license* firm*ID
	drop _merge

/* 242 firms */
	merge m:1 firmID region using "../data/ring_list020119.dta"
	*merge m:1 firmID region using "../firmleveltest24Apr24/ring_list020119.dta"
	drop _merge

/* cluster */
	/*merge m:1 firmID region using "Tests/ringfirms_only.dta"
	drop c1-c242
	rename g* g*_only
	drop _merge*/
	
	merge m:1 firmID region using "../firmleveltest/ringfirms_plus.dta"
	*merge m:1 firmID region using "../firmleveltest24Apr24/ringfirms_plus.dta"
	drop c1 - c1455
	rename g* g*_plus
	compress
	drop _merge

	
	*br tsrank partic firmID if nm_firm == "豊浦建設工業（株）"

sort firmID region fy_con
by firmID region: egen sales_mil = total( winbid * winner / 1000000 )
by firmID region: egen meanwbp = mean( win_bid_pct )
by firmID region: egen won = total( winner )

forvalues x = 2015(1)2017{
	cap drop *`x'
	by firmID region fy_con: egen sales_mil`x' = total( winbid * winner / 1000000 ) ///
					if fy_con == `x'
	by firmID region fy_con: egen meanwbp`x' = mean( win_bid_pct) if fy_con == `x'
	by firmID region fy_con: egen won`x' = total( winner ) if fy_con == `x'
	*tabstat sales_mil`x' won`x' meanwbp`x' if regionfirmTG == 1 & fy_con == `x', by(nm_firm)
}

/*実験は28 groups でする 1/23/2019*/
/*
local x = 28
graph drop _all
cap drop label
gen label = ""
	forvalues z = 1(1)`x'{
		local aa = ""
		forvalues y = 2015(1)2017{
			local stmean = ""
			local stmean_ring = ""
			di "*** year:`y'; group:`z' ***"
			sum win_bid_pct if g`x'_plus == `z' & fy_con == `y', mean
			cap noisily local stmean = string(`r(mean)' * 100, "%5.1f")
			local aa = "`aa'`stmean'"
			sum win_bid_pct if g`x'_plus == `z' & fy_con == `y' & ring_firm == 1, mean
			cap noisily local stmean_ring = string(`r(mean)' * 100, "%5.1f")
			local aa = "`aa'(`stmean_ring') "
		}
		local bb = string(`z', "%02.0f")
		replace label = "Grp.`bb'; WBP:`aa'" if g`x'_plus == `z'
	}
sort label
tab label
twoway( ///
scatter win_bid_pct date2005 if ring_firm == .)( ///
scatter win_bid_pct date2005 if ring_firm == 1), ///
	by(label, style(compact) note("wbp of ring_firms in parenthesis")) ///
	legend(/*pos(3) col(1)*/ order(2 1) lab(1 "Others") lab(2 "Ring Firms")) ///
	subtitle(, ring(0) pos(5) nobexpand size(vsmall))
graph export "../firmleveltest/Graph_Ring_Plus`x'/win_bid_pct.png", replace width(4096) as(png)
*/

/* count #firms in each group*/
	cap drop ccc ddd
	bys g28_plus regionfirmTG pref city: gen ccc = _N if regionfirmTG == 1
	bys g28_plus regionfirmTG: egen ddd = rank(-ccc), u
	*br pref city ccc ddd g28_plus if regionfirmTG == 1 & ddd < 10
	
	cap drop c_*firm
	bys g28_plus: egen c_ring_firm = total(ring_firm * regionfirmTG == 1)
	bys g28_plus: egen c_firm = total(ring_firm )

	cap drop g28TG
	egen g28TG = tag(regionfirmTG g28_plus)

	/*
	cap mkdir "../firmleveltest/Gr28_J"
	gen grouplabel = ""
	local c = 1
	foreach vv in "道央南" "道央北" "函館松前" "留萌" "稚内" "釧路・根室" "紋別・網走" ///
					"小樽・ニセコ" "茨城造園" "山形" "秋田" "島根道路" "岐阜電気" "大分造園" ///
					"新潟" "静岡三島" "岐阜・長野" "岐阜標識" "愛知・岐阜" "静岡菊川"  "奈良" ///
					"愛媛道路" "青森" "島根" "四国道路" "福井造園" "釧路造園" "徳島"{


		cap mkdir "../firmleveltest/Gr28_J/Grp`c'_`vv'"
		local list : dir "../firmleveltest/Graph_Ring_Plus28/group`c'" files "*.png"

		foreach f in `list'{
			copy "../firmleveltest/Graph_Ring_Plus28/group`c'/`f'" "../firmleveltest/Gr28_J/Grp`c'_`vv'/`f'"
		}
		replace grouplabel = "`vv'" if g28_plus == `c'
		local c = `c' + 1
	}
	*/
	format %10s pref city
	sort regionfirmTG g28_plus ring_firm
	*br nm_firm ad_firm g28_pl ring_firm c_ring firmID /*ccc ddd*/ if regionfirmTG == 1 
	*br grouplabel g28_pl c_ring c_firm firmID ///
	*	/*ccc ddd*/ if g28TG == 1 & _n < 100000

	/*結果1/23/2019、x_yが一つのペアで、xかyのどちらか一方のグループの中で、242社に含まれる企業の本店にアンケート送る*/
		/*北海道(8): 1_7 6_8 2_3 4_5; 造園(4):9_14 26_27; 東北(2):11_23; 西日本道路12_22　19_25;
	数が多い(山形/静岡三島)10_15; 17_16 18_28 13_20 /*24*/　*/
	
	
	sort rid
	cap drop g28plusTG
	egen g28plusTG = tag(g28_plus)
	replace g28plusTG = . if g28plusTG == 0
	sort g28plusTG g28_plus
	cap drop r 
	*set seed 20190208 /* <- このシードでwin_bid_pctはバランスする*/
	set seed 20190210 /* <- このシードで3つバランスする*/
	gen r = runiform() if g28plusTG == 1
	cap drop pair28
	
	/* assigning pair id form 1 to 13*/
		*北海道
		gen pair28 = 1 if g28_plus == 1 | g28_plus == 7 /*道央南（14）,紋別・網走	31*/
		replace pair28 = 2 if g28_plus == 6 | g28_plus == 8 /*釧路・根室	14,小樽・ニセコ	14*/
		replace pair28 = 3 if g28_plus == 2 | g28_plus == 3 /*道央北	8,函館松前	10*/
		replace pair28 = 4 if g28_plus == 4 | g28_plus == 5 /*留萌	4,稚内	4*/
		*造園
		replace pair28 = 5 if g28_plus == 9 | g28_plus == 14 /*茨城造園	10,大分造園	5*/
		replace pair28 = 6 if g28_plus == 26 | g28_plus == 27 /*福井造園	1,釧路造園	3*/
		*西日本道路
		replace pair28 = 7 if g28_plus == 12 | g28_plus == 22 /*島根道路	3,愛媛道路	4*/
		replace pair28 = 8 if g28_plus == 18 | g28_plus == 25 /*岐阜標識	2,四国道路	1*/
		
		*あまり、ringirmsの数の多い順に割り振り020319
		replace pair28 = 9 if g28_plus == 10 | g28_plus == 16 /*山形	32,静岡三島	24,数が多い*/
		replace pair28 = 10 if g28_plus == 11 | g28_plus == 19 /*秋田	11,愛知・岐阜	18*/
		replace pair28 = 11 if g28_plus == 17 | g28_plus == 20 /*岐阜・長野	8,静岡菊川	6*/
		replace pair28 = 12 if g28_plus == 23 | g28_plus == 28 /*青森	3,徳島	6*/
		replace pair28 = 13 if g28_plus == 13 | g28_plus == 15 /*岐阜電気	2,新潟	2*/
	order pair28

	/*乱数つかってペアのどちらのグループをtreatするか決める*/
		cap drop uuu
		cap drop treatment
		bys pair28: egen uuu = max(r * (g28plusTG == 1) )
		gen treatment = 1 if uuu == r & pair28 < .
		order uuu treatment
		cap drop treatment_group
		bys g28_plus: egen treatment_group = max(treatment)
		replace treatment_group = . if ring_firm ~= 1
		sort regionfirmTG g28_plus ring_firm
		format %30s nm_firm ad_firm
		/*br nm_firm ad_firm g28_pl ring_firm treatment_group c_ring firmID ///
			if regionfirmTG == 1 & ring_firm == 1*/
		count if treatment_group == 1 & regionfirmTG == 1
		tab pair28 if treatment_group == 1 & regionfirmTG == 1
		tab pair28 if treatment_group == . & regionfirmTG == 1 &  ring_firm == 1
		count if treatment_group == . & regionfirmTG == 1 & ring_firm == 1
	
	tabstat win_bid_pct if treatment_group == 1, by(pair28) s(n mean sd)
	tabstat win_bid_pct if treatment_group == . & ring_firm == 1, by(pair28) s(n mean sd)

	sum win_bid_pct if treatment_group == 1, d
	sum win_bid_pct if treatment_group == .& ring_firm == 1, d
	
	/* test balance*/
	gen aaa = 0 if ring_firm == 1
	replace aaa = 1 if treatment_group == 1
	ttest win_bid_pct if ring_firm == 1, by(aaa)
	ttest partic if ring_firm == 1 & regionfirmTG == 1, by(aaa)
	ttest mints1 if ring_firm == 1 & regionfirmTG == 1, by(aaa)

	keep if regionfirmTG == 1
	keep rid pair28 g28_plus treatment_group nm_firm ad_firm no_license  ///
	tsrank mints *ID partic ring_firm region
	
	tab g28_plus if treatment_group == 1
	/*
	   g28_plus |      Freq.     Percent        Cum.
	------------+-----------------------------------
			  1 |         14       13.08       13.08
			  2 |          8        7.48       20.56
			  5 |          4        3.74       24.30
			  8 |         14       13.08       37.38
			 12 |          3        2.80       40.19
			 14 |          5        4.67       44.86
			 15 |          2        1.87       46.73
			 16 |         24       22.43       69.16
			 18 |          2        1.87       71.03
			 19 |         18       16.82       87.85
			 20 |          6        5.61       93.46
			 26 |          1        0.93       94.39
			 28 |          6        5.61      100.00
	------------+-----------------------------------
		  Total |        107      100.00
	*/
	*	save "../data/Pair13_Group28_treatment.dta", replace
	
	keep if g28_plus < . & ring_firm011419 < . & pair28 < .
	keep g28_plus pair28 treatment_group
	replace treatment_group = 0 if treatment_group == .
	egen TG = tag(g28_plus)
	keep if TG == 1
	drop TG
	save ../fishertest/group_treatment, replace
	
	
	
